package com.uzai.console.service.user.impl;

import com.google.common.collect.Lists;
import com.uzai.console.dto.user.UserJoinWhiteDto;
import com.uzai.console.entity.RiskLevel;
import com.uzai.console.mapper.RiskLevelMapper;
import com.uzai.console.mapper.WechatUserMapper;
import com.uzai.console.service.user.RiskLevelService;
import com.uzai.console.service.user.WechatUserService;
import com.uzai.console.vo.user.RiskLevelVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyanyan
 */
@Service
public class RiskLevelServiceImpl  implements RiskLevelService {
    @Autowired
    private RiskLevelMapper riskLevelMapper;

    @Autowired
    private WechatUserMapper wechatUserMapper;
    @Override
    public List<RiskLevelVo> selectList() {
        List<RiskLevelVo> levels = new ArrayList<>();
        List<RiskLevel> riskLevels = riskLevelMapper.selectAll();
        if (ObjectUtils.isEmpty(riskLevels)) {
            return Lists.newArrayList();
        }
        for (RiskLevel riskLevel : riskLevels) {
            RiskLevelVo riskLevelVo = new RiskLevelVo();
            BeanUtils.copyProperties(riskLevel, riskLevelVo);
            levels.add(riskLevelVo);
        }
        return levels;
    }

    @Override
    public void joinWhitelist(UserJoinWhiteDto userJoinWhiteDto) {
        Integer riskValue = userJoinWhiteDto.getRiskValue().equals(0) ? 0 : 1;
        wechatUserMapper.updateRiskScoreById(userJoinWhiteDto.getId(), userJoinWhiteDto.getMerId(), riskValue);
    }
}
